package com.bosssoft.setp.handler;


import com.bosssoft.setp.constant.CommonConstant;
import org.springframework.stereotype.Service;
import xyz.erupt.annotation.fun.ChoiceFetchHandler;
import xyz.erupt.annotation.fun.VLModel;
import xyz.erupt.jpa.dao.EruptDao;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 单选下拉框数据源
 *
 * @author clt
 */
@Service
public class ChoiseFetchHandlerImpl implements ChoiceFetchHandler {
    @Resource
    private EruptDao eruptDao;

    private String sqlDict = "select id from e_dict where name = '?'";
    private String sqlItem = "select name from e_dict_item where erupt_dict_id = ?";

    /**
     * 根据字典名称获取字典项
     * @param params
     * @return
     */
    @Override
    public List<VLModel> fetch(String[] params) {
        Long dictId = eruptDao.getJdbcTemplate().queryForObject(sqlDict.replace("?", params[0]), Long.class);
        List<String> dictItems = eruptDao.getJdbcTemplate().queryForList(sqlItem, new Object[]{dictId}, String.class);

        List<VLModel> vlModels = new ArrayList<>();
        dictItems.forEach(item -> vlModels.add(new VLModel(item, item)));

        return vlModels;
    }
}